(function ($, doc) {
    $.init();
    
    $.plusReady(function () {
        var user = app.getState();
        var url = apiurl + 'favorite/index?uid=' + user.user_id;
        $.get(url, function (data) {
            renderlist(data);
        })

        function renderlist(data){
            document.querySelector("#fav").innerHTML = template('list-template', {
                "data": data.favorites
            });
        }

        mui("#fav").on("tap", ".fav-sub", function() {
            var fid = this.getAttribute("uid");
            var head = this.getAttribute("header");
            var fname = this.getAttribute("name");
            var phone = this.getAttribute("phone");
            var c_msg = this.getAttribute("c_msg");
            var c_img = this.getAttribute("c_img");
            var ctype = this.getAttribute("ctype");
            var cname = self.cname;
            apage = plus.webview.getWebviewById("chatbox");
            mui.fire(apage, 'SENDFAV', {fid : fid, head : head, fname : fname, phone : phone, cname:cname,c_msg:c_msg,c_img:c_img,ctype:ctype});

            plus.webview.currentWebview().close();
            mui.openWindow({
                url: 'chat_index.html',
                id: 'chatbox',
                extras: {
                    chatTitle: plus.webview.currentWebview().chatTitle,
                    chatHeader: plus.webview.currentWebview().chatHeader,
                    roomId : plus.webview.currentWebview().roomId,
                    roomType : plus.webview.currentWebview().roomType
                }
            });
        });


    })
}(mui, document));